package com.bite.springblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bite.springblog.common.exception.BlogException;
import com.bite.springblog.common.utils.BeanTransUtils;
import com.bite.springblog.common.utils.JwtUtils;
import com.bite.springblog.common.utils.SecurityUtil;
import com.bite.springblog.mapper.BlogInfoMapper;
import com.bite.springblog.mapper.UserInfoMapper;
import com.bite.springblog.pojo.daoobject.BlogInfo;
import com.bite.springblog.pojo.daoobject.UserInfo;
import com.bite.springblog.pojo.request.UserLoginRequest;
import com.bite.springblog.pojo.response.UserInfoResponse;
import com.bite.springblog.pojo.response.UserLoginResponse;
import com.bite.springblog.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private BlogInfoMapper blogInfoMapper;

    @Override
    public UserLoginResponse userLogin(UserLoginRequest userLoginRequest) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getDeleteFlag,0)
                    .eq(UserInfo::getUserName,userLoginRequest.getUserName());
        UserInfo userInfo = null;
        try {
            userInfo = userInfoMapper.selectOne(queryWrapper);
        }catch (Exception e){
            log.error("用户数据库查询错误");
        }

        //数据校验
        if(userInfo == null){
            throw new BlogException("用户不存在");
        }
//        if(!userLoginRequest.getPassword().equals(userInfo.getPassword())){
//            throw new BlogException("密码错误");
//        }
        if(!SecurityUtil.verify(userLoginRequest.getPassword(),userInfo.getPassword())){
            throw new BlogException("密码错误");
        }

        Map<String, Object> claims = new HashMap<>();
        claims.put("id",userInfo.getId());
        claims.put("name",userInfo.getUserName());
        String token = JwtUtils.getToken(claims);
        return new UserLoginResponse(userInfo.getId().toString(),token);
    }

    @Override
    public UserInfoResponse getUserInfo(String userId) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(UserInfo::getDeleteFlag,0)
                    .eq(UserInfo::getId,userId);
        try {
            UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
            UserInfoResponse retData = BeanTransUtils.trans(userInfo);
            if(retData == null){
                throw new BlogException("用户不存在");
            }
            return retData;
        }catch (Exception e){
            log.error("用户信息查询，用户不存在");
        }
        return null;
    }

    @Override
    public UserInfoResponse getAuthorInfo(String blogId) {
        LambdaQueryWrapper<BlogInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlogInfo::getDeleteFlag,0)
                    .eq(BlogInfo::getId,blogId);
        queryWrapper.select(BlogInfo::getUserId);
        BlogInfo userId = blogInfoMapper.selectOne(queryWrapper);
        if(userId.getUserId()<=0){
            log.error("作者信息获取错误");
        }
        return getUserInfo((userId.getUserId()).toString());
    }
}




